package com.rdc.qingkou.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.rdc.qingkou.R;

import java.util.ArrayList;
import java.util.List;


public class LabelRecycleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private List<String> mList;
    private static  final int sHeader = 0;
    private static final int sBOTTOM = 2;
    private static  final int sCENTER = 3;
    private    RecyclerView.ViewHolder holder;
    private List<Boolean> mflag = new ArrayList<>();
    private List<String> mLabel = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener = null;

    public interface OnItemClickListener {
        void onItemClick(View view);
    }
    static class CheckViewHolder extends RecyclerView.ViewHolder{
        CheckBox checkBox;
        public CheckViewHolder(View itemView) {
            super(itemView);
            checkBox = (CheckBox) itemView.findViewById(R.id.cb_label);

        }
    }

    static class TextViewHolder extends RecyclerView.ViewHolder{
        TextView textView;
        public TextViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.tv_addTitle);

        }
    }

    static class CustomTextViewHolder extends  RecyclerView.ViewHolder {
        TextView textView;

        public CustomTextViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.tv_label_custom);
        }
    }

    public LabelRecycleViewAdapter(List<String> mList) {
        this.mList = mList;


    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? sHeader:position == getItemCount()-1? sBOTTOM:sCENTER;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType==sHeader){
            View view =  LayoutInflater.from(parent.getContext()).inflate(R.layout.send_addlabel_item,parent,false);
            holder = new TextViewHolder(view);
        }
        else if (viewType == sCENTER){
            View view =  LayoutInflater.from(parent.getContext()).inflate(R.layout.send_labellist_item,parent,false);
            holder = new CheckViewHolder(view);
        }else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.send_custom_item,parent,false);
            holder = new CustomTextViewHolder(view);
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof TextViewHolder) {
            mflag.add(false);
            mLabel.add("");

        } else if (holder instanceof  CheckViewHolder){
            mflag.add(false);
            String string = mList.get(position);
            ((CheckViewHolder) holder).checkBox.setText(string);
            ((CheckViewHolder) holder).checkBox.setOnCheckedChangeListener(null);
            ((CheckViewHolder) holder).checkBox.setChecked(mflag.get(position));
            ((CheckViewHolder) holder).checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    mflag.set(position,b);
                }

            });
        }else {
            ((CustomTextViewHolder)holder).textView.setOnClickListener(this);
        }


    }
    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v);
        }
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public  List<Boolean> getmFlag(){
        return mflag;
    }



}
